package com.belf.web.controller.console;

import com.belf.common.annotation.Log;
import com.belf.common.core.controller.BaseController;
import com.belf.common.core.domain.AjaxResult;
import com.belf.common.core.domain.entity.SysUser;
import com.belf.common.core.page.TableDataInfo;
import com.belf.common.enums.BusinessType;
import com.belf.common.utils.SecurityUtils;
import com.belf.common.utils.poi.ExcelUtil;
import com.belf.console.domain.DataSyncConf;
import com.belf.console.service.IDataSyncConfService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * cdc 配置Controller
 * 
 * @author belf
 * @date 2024-07-19
 */
@RestController
@RequestMapping("/console/syncConf")
public class DataSyncConfController extends BaseController
{
    @Autowired
    private IDataSyncConfService dataSyncConfService;

    /**
     * 查询cdc 配置列表
     */
    @PreAuthorize("@ss.hasPermi('console:syncConf:list')")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(@RequestBody DataSyncConf dataSyncConf)
    {
        // 管理员拥有所有权限
        if (!SysUser.isAdmin(SecurityUtils.getUserId())){
            dataSyncConf.setCreateBy(getUsername());
        }
        List<DataSyncConf> list = dataSyncConfService.selectDataSyncConfList(dataSyncConf);
        long count = dataSyncConfService.selectDataCount(dataSyncConf);
        return getDataTablePost(list,count);
    }

    /**
     * 导出cdc 配置列表
     */
    @PreAuthorize("@ss.hasPermi('console:syncConf:export')")
    @Log(title = "cdc 配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DataSyncConf dataSyncConf)
    {
        List<DataSyncConf> list = dataSyncConfService.selectDataSyncConfList(dataSyncConf);
        ExcelUtil<DataSyncConf> util = new ExcelUtil<DataSyncConf>(DataSyncConf.class);
        util.exportExcel(response, list, "cdc 配置数据");
    }

    /**
     * 获取cdc 配置详细信息
     */
    @PreAuthorize("@ss.hasPermi('console:syncConf:query')")
    @PostMapping("/getInfo")
    @ResponseBody
    public AjaxResult getInfo(@RequestBody DataSyncConf dataSyncConf)
    {
        return success(dataSyncConfService.selectDataSyncConfById(dataSyncConf.getId()));
    }

    /**
     * 新增cdc 配置
     */
    @PreAuthorize("@ss.hasPermi('console:syncConf:add')")
    @Log(title = "cdc 配置", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult add(@RequestBody DataSyncConf dataSyncConf)
    {
        dataSyncConf.setCreateBy(getUsername());
        dataSyncConf.setUpdateBy(getUsername());
        return toAjax(dataSyncConfService.insertDataSyncConf(dataSyncConf));
    }

    /**
     * 修改cdc 配置
     */
    @PreAuthorize("@ss.hasPermi('console:syncConf:edit')")
    @Log(title = "cdc 配置", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult edit(@RequestBody DataSyncConf dataSyncConf)
    {
        dataSyncConf.setUpdateBy(getUsername());
        return toAjax(dataSyncConfService.updateDataSyncConf(dataSyncConf));
    }

    /**
     * 删除cdc 配置
     */
    @PreAuthorize("@ss.hasPermi('console:syncConf:remove')")
    @Log(title = "cdc 配置", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    public AjaxResult remove(@RequestBody DataSyncConf dataSyncConf)
    {
        return toAjax(dataSyncConfService.deleteDataSyncConfByIds(dataSyncConf.getIdArray()));
    }
}
